using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Input;
using System.Collections.Generic;
using System.Windows.Navigation;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using AdRotator;
using Microsoft.Advertising.Mobile.UI;

using Common.Library;
using Common.Utilities;

namespace Common.Utilities
{
	public class PageHelper
	{
		public UIElement OldControl { get; private set; }
		private double oldControlOpacity;
		private Color oldTrayBg;
		private Color oldTrayFg;
		
		private PhoneApplicationPage page;
		private bool disableInteractions;

        public static void NavigateToMainPage(object sender, MouseEventArgs e)
        {
            NavigateTo(UtilitiesResources.MainPageUri);
        }
       
		public static PageHelper InactivatePage()
		{
			return InactivatePage(false);
		}

		public static PageHelper InactivatePage(bool makePageInactive)
		{
			var s = new PageHelper();
			s.SetPageStatus(makePageInactive);
			return s; 
		}
        public static void NavigateToOnlineImagesPage(string page)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml", UriKind.Relative));
        }
        public static void NavigateToUserUploadPage(string page)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml", UriKind.Relative));
        }
       
        public static PhoneApplicationFrame RootApplicationFrame
        {
            get { return (PhoneApplicationFrame)Application.Current.RootVisual; }
        }

        public static PhoneApplicationPage RootApplicationPage
        {
            get { return (PhoneApplicationPage)((PhoneApplicationFrame)Application.Current.RootVisual).Content; }
        }
        public static void AdControlForPro(Grid ContainerGrid, StackPanel AdContainer)
        {
            AdControlForPro(ContainerGrid, AdContainer, 1);
        }
        public static void AdControlForPro(Grid ContainerGrid, StackPanel AdContainer, int RowPosition)
        {
            if (!UtilitiesResources.ShowAdControl)
            {
                if (ContainerGrid.RowDefinitions.Count > RowPosition)
                {
                    RowDefinition myrow = ContainerGrid.RowDefinitions[RowPosition];
                    ContainerGrid.RowDefinitions.Remove(myrow);
                }
            }

        }
        public static void LoadAdControl(Grid ContainerGrid, StackPanel AdContainer)
        {
            LoadAdControl(ContainerGrid, AdContainer, 1);
        }

        public static void LoadAdControl(Grid ContainerGrid, StackPanel AdContainer, int RowPosition)
        {
            if (!UtilitiesResources.ShowAdControl)
            {
                if (ContainerGrid.RowDefinitions.Count > RowPosition)
                {
                    RowDefinition myrow = ContainerGrid.RowDefinitions[RowPosition];
                    ContainerGrid.RowDefinitions.Remove(myrow);
                }
            }
            else
            {
                if (UtilitiesResources.ShowAdRotator)
                {
                    AdRotatorControl adControl = new AdRotatorControl();
                    adControl.Width = 480;
                    adControl.Height = 80;
                    adControl.Visibility = (Visibility)0;
                    adControl.BorderBrush = Resources.TransparentBrush;
                    adControl.Foreground = Resources.PhoneForegroundBrush;
                    adControl.Background = Resources.PhoneBackgroundBrush;
                    if(ResourceHelper.AppName==Apps.Web_Tile.ToString())
                           adControl.DefaultSettingsFileUri = new Uri("/webslice;component/DefaultData/defaultAdSettings.xml", UriKind.RelativeOrAbsolute);
                            else
                    adControl.DefaultSettingsFileUri = new Uri("/" + ResourceHelper.ProjectName.Replace(" ", "") + ";component/DefaultData/defaultAdSettings.xml", UriKind.RelativeOrAbsolute);
                    adControl.SettingsUrl = AppResources.AdRotatorSettingsUrl;
                    AdContainer.Children.Add(adControl);
                    ((AdRotator.AdRotatorControl)AdContainer.Children[0]).Invalidate();
                }
                else
                {
                    AdControl adControl = new AdControl();
                    adControl.AdUnitId = UtilitiesResources.AdControlAdUnitID;
                    adControl.ApplicationId = UtilitiesResources.AdControlApplicationID;
                    adControl.Width = 480;
                    adControl.Height = 80;
                    adControl.Visibility = (Visibility)0;
                    adControl.BorderBrush = Resources.TransparentBrush;
                    adControl.Foreground = Resources.PhoneForegroundBrush;
                    adControl.Background = Resources.PhoneBackgroundBrush;
                    adControl.IsAutoRefreshEnabled = true;

                    AdContainer.Children.Add(adControl);
                }
            }
        }
              
		internal void SetPageStatus(bool disablePageInteractions)
		{
			disableInteractions = disablePageInteractions;

            page = RootApplicationPage;
            OldControl = RootApplicationPage;
            oldControlOpacity = OldControl.Opacity;

            oldTrayBg = SystemTray.BackgroundColor;
            oldTrayFg = SystemTray.ForegroundColor;

            if (disableInteractions)
                OldControl.Opacity = 0.325;
            else
                OldControl.Opacity = 1;

            SystemTray.BackgroundColor = Resources.PhoneBackgroundColor;
            SystemTray.ForegroundColor = Resources.PhoneForegroundColor;

            if (disableInteractions)
                page.IsEnabled = false;
            else
                page.IsEnabled = true;
		}

        public static DependencyObject GetDependencyObjectFromVisualTree(DependencyObject startObject, Type type)
        {
            DependencyObject parent = startObject;
            int i = 0;
            while (parent != null)
            {
                if (type.IsInstanceOfType(parent))
                    break;
                else
                    parent = VisualTreeHelper.GetParent(parent);
                i++;
            }

            return parent;
        }

        public static IEnumerable<DependencyObject> GetChildsRecursive(DependencyObject root)
        {
            List<DependencyObject> elts = new List<DependencyObject>();
            try
            {
                elts.Add(root);

                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
                {
                    elts.AddRange(GetChildsRecursive(VisualTreeHelper.GetChild(root, i)));
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in GetChildsRecursive Method In Details file", ex);
            }
            return elts;
        }

        public static void RemoveEntryFromBackStack(string pageName)
        {
            try
            {
                int NavigationID = 0;

                if (PageHelper.RootApplicationFrame.Source.ToString().Contains("myid"))
                {
                    int indexOfLastEqualtoOperator = PageHelper.RootApplicationFrame.Source.ToString().LastIndexOf('=') + 1;
                    string myID = PageHelper.RootApplicationFrame.Source.ToString().Substring(indexOfLastEqualtoOperator, 1);

                    int myIntID = 0;

                    if(Int32.TryParse(myID, out myIntID))
                        NavigationID = myIntID;

                    AppSettings.NavigationID = Convert.ToBoolean(NavigationID);
                }

                var backStackList = PageHelper.RootApplicationFrame.BackStack.ToList();

                foreach (var page in backStackList)
                {
                    if (!pageName.Contains("MainPage"))
                    {
                        if (page.Source.ToString().Contains(pageName))
                        {
                            PageHelper.RootApplicationFrame.RemoveBackEntry();
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }

        }

        public static void NavigateTo(Uri uri)
        {
            PageHelper.RootApplicationFrame.Navigate(uri);
        }

        public static void NavigateTo(string page, string navigationID)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml?myid=" + navigationID, UriKind.Relative));
        }
    
        public static void NavigateToDetailPage(string page, string ShowId)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml?id=" + ShowId, UriKind.Relative));
        }
       
        public static void NavigateToHistoryPage(string page)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml", UriKind.Relative));
        }
        public static void NavigateToPersonGalleryPopupPage(string page)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml", UriKind.Relative));
        }
        public static void NavigateToOnlineWebTile(string page)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml", UriKind.Relative));
        }
        public static void NavigateToFavoritePage(string page)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml", UriKind.Relative));
        }
        public static void NavigateToRingTonePage(string page, string link,string showid,string chapterno)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml?link=" + link + "&showid=" + showid + "&chapterno=" + chapterno, UriKind.Relative));
        }
      
        public static void NavigateToLiricsShowPage(string page, string Title)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml?id=" + Title, UriKind.Relative));
        }
        public static void NavigateToCastDetailPage(string page, string ShowId)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml?id=" + ShowId, UriKind.Relative));
        }
        public static void NavigateToDownloadedImagePage(string page)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml", UriKind.Relative));
        }
        public static void NavigateToPlayDownloadedVideos(string page, string ShowId)
        {
            NavigateTo(new Uri("/Views/" + page + ".xaml?ShowId=" + ShowId, UriKind.Relative));
        }

        public static string GetFeedbackMailMessage(NavigationContext context)
        {
            string VideoTitle = string.Empty;
            string LinkType = string.Empty;
            string VideoUrl = string.Empty;
            string ShowTitle = string.Empty;
            string FeedbackMessage = string.Empty;

            context.QueryString.TryGetValue("chno", out VideoTitle);
            context.QueryString.TryGetValue("LinkType", out LinkType);
            context.QueryString.TryGetValue("uri", out VideoUrl);
            context.QueryString.TryGetValue("title", out ShowTitle);
            if (context.QueryString.TryGetValue("chno", out VideoTitle) && context.QueryString.TryGetValue("LinkType", out LinkType) && context.QueryString.TryGetValue("uri", out VideoUrl) && context.QueryString.TryGetValue("title", out ShowTitle))
            {

                FeedbackMessage = "Movie Name : " + ShowTitle + "\n" + VideoTitle + " \n " + "http://m.youtube.com/watch?v=" + VideoUrl + "\n";
            }
            return FeedbackMessage;
        }
        public static string GetFeedbackMailMessageForAudio(NavigationContext context)
        {
            string VideoTitle = string.Empty;
            string LinkType = string.Empty;
            string VideoUrl = string.Empty;
            string ShowTitle = string.Empty;
            string FeedbackMessage = string.Empty;
            context.QueryString.TryGetValue("chno", out VideoTitle);

            context.QueryString.TryGetValue("LinkType", out LinkType);
            context.QueryString.TryGetValue("uri", out VideoUrl);
            context.QueryString.TryGetValue("title", out ShowTitle);
            if (context.QueryString.TryGetValue("chno", out VideoTitle) && context.QueryString.TryGetValue("LinkType", out LinkType) && context.QueryString.TryGetValue("uri", out VideoUrl) && context.QueryString.TryGetValue("title", out ShowTitle))
            {

                FeedbackMessage = "Movie Name : " + ShowTitle + "\n" + VideoTitle + " \n " +  VideoUrl + "\n";
            }
            return FeedbackMessage;
        }
        public static void SetSelectedItemForegroundColor(ListBox listBox, string itemTitle)
        {
            IEnumerable<DependencyObject> cboxes = PageHelper.GetChildsRecursive(listBox);

            foreach (DependencyObject obj in cboxes.OfType<TextBlock>())
            {
                Type type = obj.GetType();
                if (type.Name == "TextBlock")
                {
                    TextBlock cb = obj as TextBlock;
                    
                    
                        if (cb.Text == itemTitle)
                            cb.Foreground = new SolidColorBrush(Color.FromArgb(100, 24, 255, 250));
                    
                }
            }
        }
        
	}
}